package com.dwjm.stu.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionDatabaseForMySQLUseJdbc {
    //连接数据库驱动
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    //连接数据库的URL
    private static final String URL = "jdbc:mysql://localhost:3306/shopping?&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
    //连接数据库的账号
    private static final String USER="root";
    //连接数据库的密码
    private static final String PASSWORD="123456";
    //定义数据库连接对象
    static Connection connection= null;
    //连接数据库的函数
    public static Connection getConnection() {
        try{
            //我们期望执行的程序
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(URL,USER,PASSWORD);
            return connection;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    //测试数据库是否可以连接
    public static void main(String[] args) {
        System.out.println(getConnection() == null);
    }
}


